package cn.mastor.basic.util;

import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.client.producer.TransactionSendResult;

import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageHeaders;
import org.springframework.messaging.support.GenericMessage;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Service;

import javax.mail.MessagingException;
import java.util.HashMap;

/**
 * @author: zhangzengxiu * @createDate: 2022/4/25
 */
@Service
@Slf4j
public class ProducerMsg {
    @Autowired
    private RocketMQTemplate rocketMQTemplate;

    /**
     * 发事务消息 * * @param msg * @return
     */
    public void sendMessage(String message) {
        Message<String> build = MessageBuilder.withPayload(message).build();
        rocketMQTemplate.send("hello_topic",build);
        log.info("普通消息发送完成：message = {}", message);
    }
    public void syncSendDelayMessage(String messages){
        HashMap<String, Object> maps = new HashMap<>();
        Message<String> message = new GenericMessage<String>(messages, new MessageHeaders(maps));
        SendResult sendResult = rocketMQTemplate.syncSend("hello_topic", message, 3000, 10);
        log.info(sendResult.toString());
    }



}